package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Enumeration;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Connectivity.ConnectionManager;

public class CallAssign extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1457680919255724001L;

	/**
	 * Constructor of the object.
	 */
	public CallAssign() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
	try
	{ Connection con=ConnectionManager.getConnection();
		String assigndetails="";
		String ParameterNames = "";
		int tokcnt=0;
		for(Enumeration e = request.getParameterNames(); e.hasMoreElements(); )
		{
			ParameterNames = (String)e.nextElement();
			//out.println(ParameterNames + "=<br/>");
			String value=request.getParameter(ParameterNames);
			 if(value.equalsIgnoreCase("0") || value.equalsIgnoreCase("Assign")){}
			 else
			{
			assigndetails=assigndetails+"$"+request.getParameter(ParameterNames);
			
			StringTokenizer st=new StringTokenizer((request.getParameter(ParameterNames)),"#"); 
			//int callentry_id=Integer.parseInt(""+(st.nextToken("1")));
			//int emp_id=Integer.parseInt(""+(st.nextToken("2")));
			while(st.hasMoreTokens())
			{
			String callentry_id=st.nextToken();
 			String emp_id=st.nextToken();
						
			String updateq="UPDATE callentry set call_status='ASSIGNED' where callentry_id="+callentry_id+" and status='V'";	
			PreparedStatement uprs=con.prepareStatement(updateq);
			boolean k=uprs.execute();
			 HttpSession session = request.getSession(true);
		    String created_by=""+(session.getAttribute("uname"));
			String inassign="INSERT INTO call_assigned(callentry_id,employee_id,created_by)";
			inassign=inassign+"values (?,?,?)";
			PreparedStatement inasspr=con.prepareStatement(inassign);
			inasspr.setInt(1,Integer.parseInt(callentry_id));
			inasspr.setInt(2,Integer.parseInt(emp_id));
			inasspr.setString(3,created_by);
			boolean s=inasspr.execute();
            if(s)
            {response.sendRedirect("/Genius/form/general/CallAssign.jsp?status='I'");}    
			
			

			
			
			
			}
			
			
			
			
			
			
			
			
			
			
			}
//			out.println(problem + "<br/>");
			 
 
		}response.sendRedirect("/Genius/form/general/CallAssign.jsp?status=I");

	}
	catch(Exception e)
	{e.printStackTrace();	}
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
